package com.ruoyi.web.controller.ufox;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ufox.domain.BisDevice;
import com.ruoyi.ufox.service.IBisDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 蔬菜机Controller
 * 
 * @author ufoxs
 * @date 2022-08-15
 */
@RestController
@RequestMapping("/bussiness/device")
public class BisDeviceController extends BaseController
{
    @Autowired
    private IBisDeviceService bisDeviceService;

    /**
     * 查询蔬菜机列表
     */
    @PreAuthorize("@ss.hasPermi('bussiness:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(BisDevice bisDevice)
    {
        startPage();
        List<BisDevice> list = bisDeviceService.selectBisDeviceList(bisDevice);
        return getDataTable(list);
    }

    /**
     * 导出蔬菜机列表
     */
    @PreAuthorize("@ss.hasPermi('bussiness:device:export')")
    @Log(title = "蔬菜机", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BisDevice bisDevice)
    {
        List<BisDevice> list = bisDeviceService.selectBisDeviceList(bisDevice);
        ExcelUtil<BisDevice> util = new ExcelUtil<BisDevice>(BisDevice.class);
        util.exportExcel(response, list, "蔬菜机数据");
    }

    /**
     * 获取蔬菜机详细信息
     */
    @PreAuthorize("@ss.hasPermi('bussiness:device:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId)
    {
        return AjaxResult.success(bisDeviceService.selectBisDeviceByDeviceId(deviceId));
    }

    /**
     * 新增蔬菜机
     */
    @PreAuthorize("@ss.hasPermi('bussiness:device:add')")
    @Log(title = "蔬菜机", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BisDevice bisDevice)
    {
        if (this.bisDeviceService.checkDeviceCode(bisDevice.getDeviceCode(),null)==true) {
            return AjaxResult.error(500,"设备编号已经存在");
        }
        return toAjax(bisDeviceService.insertBisDevice(bisDevice));
    }

    /**
     * 修改蔬菜机
     */
    @PreAuthorize("@ss.hasPermi('bussiness:device:edit')")
    @Log(title = "蔬菜机", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BisDevice bisDevice)
    {
        if (this.bisDeviceService.checkDeviceCode(bisDevice.getDeviceCode(),bisDevice.getDeviceId().intValue())==true) {
            return AjaxResult.error(500,"设备编号已经存在");
        }
        return toAjax(bisDeviceService.updateBisDevice(bisDevice));
    }

    /**
     * 删除蔬菜机
     */
    @PreAuthorize("@ss.hasPermi('bussiness:device:remove')")
    @Log(title = "蔬菜机", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds)
    {
        return toAjax(bisDeviceService.deleteBisDeviceByDeviceIds(deviceIds));
    }
}
